﻿Imports BusinessLayer
Imports EntityLayer
Imports System.Data

Partial Class ListaSalidaProducto
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Call ListarGuias()
        End If
    End Sub

    Private Sub ListarGuias()
        dgDatos.DataSource = GuiaSalidaBL.ListarTodo
        dgDatos.DataBind()
    End Sub

    Private Sub MostrarProductos(ByVal strCodigo As String)
        Try
            lblProducto.Text = "Productos de la Guia de Salida " & strCodigo.PadLeft(4, "0")
            dgListaProducto.DataSource = DetalleGuiaSalidaBL.ListarxCodigo(CInt(strCodigo))
            dgListaProducto.DataBind()
        Catch ex As Exception

        End Try

    End Sub

    Private Sub EliminarGuia(ByVal strCodigo As String)
        'Adicionando al stock las cantidades antes de eliminar
        Dim dt As DataTable = DetalleGuiaSalidaBL.ListarxCodigo(CInt(strCodigo)).Tables(0)
        Dim j As Integer
        For j = 0 To dt.Rows.Count - 1
            ProductoBL.ActualizarStockActual(dt.Rows(j)("codigo").ToString, CInt(dt.Rows(j)("cantidad").ToString), "I")
        Next
        '.........................................................
        GuiaBL.Eliminar(CInt(strCodigo), "S")
        dgDatos.DataSource = GuiaSalidaBL.ListarTodo
        dgDatos.DataBind()
        lblProducto.Text = ""
        dgListaProducto.DataSource = Nothing
        dgListaProducto.DataBind()
    End Sub

    Private Sub EditarGuia(ByVal strCodigo As String)
        Response.Redirect("SalidaProducto.aspx?strCodigo=" & strCodigo)
    End Sub

    Protected Sub btnNuevo_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
        Response.Redirect("SalidaProducto.aspx")
    End Sub

    Protected Sub dgDatos_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
        Try
            Dim index As Integer = Integer.Parse(e.CommandArgument.ToString())
            Dim row As GridViewRow = dgDatos.Rows(index)
            Dim key As DataKey = dgDatos.DataKeys(row.RowIndex)


            If e.CommandName = "Prod" Then
                MostrarProductos((key(0).ToString()))
            End If
            If e.CommandName = "Edt" Then
                EditarGuia((key(0).ToString()))
            End If
            If e.CommandName = "Del" Then
                EliminarGuia((key(0).ToString()))
            End If
        Catch ex As Exception

        End Try
    End Sub
End Class
